const express = require("express")
const dynamicRecord = express.Router();
const { recommendTable: filesbase } = require("../database/filesbase");


dynamicRecord.get("/:num", async (req, res, next) => {
     let data = await filesbase.find({
          weight: { $lt: 4 }
     }).sort({ dataTime: -1 }).skip(Number(req.params.num)).limit(8);
     res.send(data)
})

dynamicRecord.get("/search/:num", async (req, res, next) => {//返回动态浏览次数从多到少
     let data = await filesbase.find({
          weight: { $lt: 4 }
     }).sort({
           skim: -1,
          dataTime: -1  }).skip(Number(req.params.num)).limit(8);
     res.send(data)
})

dynamicRecord.get("/skimadd/:id/:time", async (req, res, next) => {//动态被查看浏览量+1

     let data = await filesbase.find(
          {
               useID: req.params.id,
               dataTime: req.params.time
          })

     data[0].skim += 1
     console.log(data[0].skim);

     await filesbase.updateOne({
          useID: req.params.id,
          dataTime: req.params.time
     }, { $set: { skim: data[0].skim } })
     
     res.send("动态+1")

})

module.exports = dynamicRecord